package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 喜欢对方
    @Override
    public void addUserlike(UserLike userLike) {
        // 1.先查询是否已经喜欢了对象
        boolean exists = mongoTemplate.exists(Query.query(Criteria.where("userId").is(userLike.getUserId()).and("likeUserId").is(userLike.getLikeUserId())), UserLike.class);
        // 2.没有喜欢过，添加本次记录
        if (!exists) {
            mongoTemplate.save(userLike);
        }
    }

    // 查询是否为相互喜欢
    @Override
    public Boolean isMutualLike(Long userId, Long userLikeId) {
        // 1.你喜欢对方
        boolean exists1 = mongoTemplate.exists(Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(userLikeId)), UserLike.class);
        // 2.对方喜欢你
        boolean exists2 = mongoTemplate.exists(Query.query(Criteria.where("userId").is(userLikeId).and("likeUserId").is(userId)), UserLike.class);
        return exists1 && exists2;
    }

    // 不喜欢对方
    @Override
    public void removeUserLike(Long userId, Long userLikeId) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(userLikeId)), UserLike.class);
    }

    // 统计数量
    @Override
    public UserLikeCountVo findCountVo(Long userId) {
        //  - 相互喜欢: 查询好友列表 (  从`friend`表中根据`userId`查询我的所有好友数量  )
        long eachLoveCount = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), Friend.class);
        //  - 喜欢: 查询你喜欢的用户 (  从`user_like`表中根据`userId`查询所有我喜欢的用户数量  )
        long loveCount = mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        //  - 粉丝: 查询别人喜欢你的用户 (  从`user_like`表中根据`likeUserId`查询所有喜欢我的用户数量 )
        long fanCount = mongoTemplate.count(Query.query(Criteria.where("likeUserId").is(userId)), UserLike.class);
        return new UserLikeCountVo(eachLoveCount, loveCount, fanCount);
    }

    // 我的详情
    @Override
    public PageBeanVo findMineUserLikeVoByPage(Long userId, Integer type, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1) * pageSize;
        switch (type) {
            case 1: {  //  - 相互喜欢: 查询好友列表 ( 从`friend`表中根据`userId`分页查询我的好友 )
                // 1-1 查询list和count
                Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
                List<Friend> friendList = mongoTemplate.find(query, Friend.class);
                long count = mongoTemplate.count(query, Friend.class);
                // 1-2 封装userLikeVo
                List<UserLikeVo> voList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(friendList)) {
                    for (Friend friend : friendList) {
                        // 创建vo
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(friend.getFriendId()); // 设置对方用户id

                        // 添加到集合
                        voList.add(vo);
                    }
                }
                // 1-3 返回分页对象
                return new PageBeanVo(pageNum, pageSize, count, voList);
            }
            case 2: { //  - 喜欢: 查询你喜欢的用户 ( 从`user_like`表中根据`userId`分页查询我喜欢的用户 )
                // 1-1 查询list和count
                Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
                List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
                long count = mongoTemplate.count(query, UserLike.class);
                // 1-2 封装userLikeVo
                List<UserLikeVo> voList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(userLikeList)) {
                    for (UserLike userLike : userLikeList) {
                        // 创建vo
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userLike.getLikeUserId()); // 对方用户id
                        // 添加到集合中
                        voList.add(vo);
                    }
                }
                // 1-3 返回分页对象
                return new PageBeanVo(pageNum, pageSize, count, voList);
            }
            case 3: { //  - 粉丝: 询别人喜欢你的用户 ( 从`user_like`表中根据`likeUserId`分页查询喜欢我的用户 )
                // 1-1 查询list和count
                Query query = Query.query(Criteria.where("likeUserId").is(userId)).skip(index).limit(pageSize);
                List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
                long count = mongoTemplate.count(query, UserLike.class);
                // 1-2 封装userLikeVo
                List<UserLikeVo> voList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(userLikeList)) {
                    for (UserLike userLike : userLikeList) {
                        // 创建vo
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userLike.getUserId());// 获取粉丝的用户id
                        // 判断双方是否为相互喜欢
                        if (isMutualLike(userId, userLike.getUserId())) {
                            vo.setAlreadyLove(true);
                        }
                        // 添加到集合中
                        voList.add(vo);
                    }
                }
                // 1-3 返回分页对象
                return new PageBeanVo(pageNum, pageSize, count, voList);
            }
            case 4: {//  - 谁看过我: 查询访客列表( 从`visitor`表中根据`userId`分页查询访问过的用户 )
                // 1-1 查询list和count
                Query query = Query.query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("date")))
                        .skip(index).limit(pageSize);
                List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
                long count = mongoTemplate.count(query, Visitor.class);
                // 1-2 封装userLikeVo
                List<UserLikeVo> voList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(visitorList)) {
                    for (Visitor visitor : visitorList) {
                        // 创建vo
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(visitor.getVisitorUserId()); // 访客用户id
                        vo.setMatchRate(visitor.getScore().intValue()); // 匹配度（缘分值）

                        // 添加vo到集合中
                        voList.add(vo);
                    }
                }
                // 1-3 返回分页对象
                return new PageBeanVo(pageNum, pageSize, count, voList);
            }
        }
        // 不属于四种类型
        return new PageBeanVo();
    }
}
